package leetcode.tree_problem;

/**
 * 543. 二叉树的直径
 * https://leetcode-cn.com/problems/diameter-of-binary-tree/
 */
public class DiameterOfBinaryTree {
    int max;
    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null || (root.left == null && root.right == null)){
            return 0;
        }
        height(root);
        return max;


    }
    public int height (TreeNode root){
        if (root == null){
            return 0;
        }

        int left = height(root.left);
        int right = height(root.right);

        max = Math.max(max , left + right);
        return 1 + Math.max(left , right);
    }
}
